﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BorrowBook : System.Web.UI.Page
{
    ArrayList aryBorrowed = new ArrayList();//列表用来存放预借信息

    /// <summary>
    /// 页面载入前
    /// </summary>
    public void Page_PreInit()
    {
        if (Session["myTheme"] != null)//判断会话中主题是否为空
        {
            this.Theme = Session["myTheme"].ToString();//设置页面主题
        }
    }

    /// <summary>
    /// 页面载入
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserID"] == null)//判断会话中用户信息是否为空
        {
            //弹出错误信息，并转到登陆页面
            this.Response.Write("<script>alert('" + "请先登陆" + "');</script>");
            this.Response.Output.WriteLine("<script language=\"javascript\">");
            this.Response.Output.WriteLine("parent.window.location=\"" + this.Request.ApplicationPath + "/Default.aspx\"");
            this.Response.Output.WriteLine("</script>");
        }

        if (Session["Borrowed"] != null)//判断会话中预借信息是否为空
        {
            aryBorrowed = (ArrayList)(Session["Borrowed"]);//从会话中取出预借信息并填充列表
            
            //根据预借信息拼接查询字符串
            string strBookID = "";
            string[] col ={ "BookID", "ISBN", "BookName", "Author", "PublishDate","Publisher" };
            string strCond = "";
            foreach (object item in aryBorrowed)
            {
                strBookID = item.ToString();
                strCond += "BookID='" + item.ToString() + "' or ";
            }
            strCond += "BookID=''";

            DataTable dt = DAL.DbAccess.Select("TBL_BookInfo", col, strCond).Tables[0];//查询信息并存放到数据表

            //从查询结果中取出信息并生成列
            ButtonField s = new ButtonField();
            s.ButtonType = ButtonType.Link;
            s.DataTextField = dt.Columns["BookID"].ToString();
            s.CommandName = "Select";
            s.HeaderText = "图书编号";
            GridView1.Columns.Add(s);

            //从查询结果中取出信息并生成列
            BoundField bFild = new BoundField();
            bFild.DataField = dt.Columns["ISBN"].ToString();
            bFild.HeaderText = "ISBN";
            GridView1.Columns.Add(bFild);

            //从查询结果中取出信息并生成列
            bFild = new BoundField();
            bFild.DataField = dt.Columns["BookName"].ToString();
            bFild.HeaderText = "书名";
            GridView1.Columns.Add(bFild);

            //从查询结果中取出信息并生成列
            bFild = new BoundField();
            bFild.DataField = dt.Columns["Author"].ToString();
            bFild.HeaderText = "作者";
            GridView1.Columns.Add(bFild);

            //从查询结果中取出信息并生成列
            bFild = new BoundField();
            bFild.DataField = dt.Columns["PublishDate"].ToString();
            bFild.HeaderText = "出版日期";
            GridView1.Columns.Add(bFild);

            //从查询结果中取出信息并生成列
            bFild = new BoundField();
            bFild.DataField = dt.Columns["Publisher"].ToString();
            bFild.HeaderText = "出版社";
            GridView1.Columns.Add(bFild);

            GridView1.DataSource = dt;//设置列表数据源
            GridView1.DataBind();//绑定列表
            string[] key ={ "BookID" };
            GridView1.DataKeyNames = key;//设置主键

        }
    }

    /// <summary>
    /// 借书确定按钮点击
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnConfirm1_Click(object sender, EventArgs e)
    {
        string strChooseID;//存放借书的ID
        for (int i = 0; i < GridView1.Rows.Count;i++)//遍历各行
        {
            strChooseID = (GridView1.DataKeys[i].Value.ToString());//设置该行图书ID
            GridViewRow row = GridView1.Rows[i];//存储该行
            CheckBox ok = (CheckBox)(row.Cells[0].FindControl("chk"));//寻找选择控件
            if (ok.Checked)//如果选择已打钩
            {
                if (BLL.CallProc.IsBorrowed(strChooseID))//查询本书是否被借
                {
                    if (BLL.CallProc.BorrowBook(Session["UserID"].ToString(), strChooseID))//尝试借书
                    {
                        this.Response.Write("<script>alert('" + "成功借阅：" + row.Cells[3].Text + "');</script>");//弹出成功信息
                    }
                }
                else//本书已被借走
                {
                    this.Response.Write("<script>alert('" + row.Cells[3].Text + "已被借走" + "');</script>");//弹出提示
                    GridView1.DeleteRow(i);
                }
            }
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
}
